//
//  SurGroupViewController.m
//  wAudioShare
//
//  Created by 赵帅 on 14-1-11.
//  Copyright (c) 2014年 赵帅. All rights reserved.
//

#import "SurGroupViewController.h"
#import "SurGroupObject.h"
#import "SurGroupOperationViewController.h"
#import "SurDataShareObj.h"

@interface SurGroupViewController ()
<GroupOperationDatasource,GroupOperationDelegate,serviceAddAudioToGroupDelegate,SurDataDelegate>
{
    
}

@property (assign) IBOutlet UITableView * tableView;

@end

@implementation SurGroupViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [SurDataShareObj sharedInstance].delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navigation methods
-(NSString *)navigationBarTitle
{
    return @"Surrounder";
}


#pragma mark -
#pragma mark UITableView Delegate & Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[SurDataShareObj sharedInstance].groupArray count]+1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == [[SurDataShareObj sharedInstance].groupArray count]) return 0;
    return 45;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section == [[SurDataShareObj sharedInstance].groupArray count])
    {
        return nil;
    }
    
    UIView * av = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
    av.backgroundColor = [UIColor lightGrayColor];
    
    UIImageView * imagev = [[UIImageView alloc] initWithFrame:CGRectMake(5, 7, 30, 30)];
    imagev.image = [UIImage imageNamed:@"multiroom_group"];
    [av addSubview:imagev];
    
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:17];
    SurGroupObject * obj = [SurDataShareObj sharedInstance].groupArray[section];
    titleLabel.frame = CGRectMake(45, 0, 215, 45);
    titleLabel.text = obj.groupName;
    [av addSubview:titleLabel];
    
    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(self.tableView.frame.size.width-52, 5, 45, 45)];
    [button setImage:[UIImage imageNamed:@"multiroom_add"] forState:UIControlStateNormal];
    [button setImageEdgeInsets:UIEdgeInsetsMake(6, 10, 14, 10)];
    button.tag = section;
    [button addTarget:self action:@selector(addDeviceButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [av addSubview:button];
    
    return av;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section != [[SurDataShareObj sharedInstance].groupArray count])
    {
        SurGroupObject * obj = [[SurDataShareObj sharedInstance].groupArray objectAtIndex:section];
        return [obj.audioGroup count];
    }
    else
    {
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    else
    {
        for(UIView * subview in [cell.contentView subviews])
        {
            [subview removeFromSuperview];
        }
    }
    
    UILabel * titleLabel = [[UILabel alloc] init];
    
    // Configure the cell.
    if(indexPath.section == [[SurDataShareObj sharedInstance].groupArray count])
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        titleLabel.font = [UIFont boldSystemFontOfSize:17];
        titleLabel.frame = CGRectMake(self.view.frame.size.width/2-50, 10, 250, 40);
        titleLabel.text = @"添加新预设组";
    }
    else
    {
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        
        SurGroupObject * obj = [SurDataShareObj sharedInstance].groupArray[indexPath.section];
        titleLabel.frame = CGRectMake(65, 10, 195, 40);
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.text = obj.audioGroup[indexPath.row];
        titleLabel.font = [UIFont systemFontOfSize:15];
        
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-55, 5, 50, 50)];
        [button setImage:[UIImage imageNamed:@"multiroom_delete"] forState:UIControlStateNormal];
        [button setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
        [button addTarget:self action:@selector(deleteDeviceButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button];
        
        UIImageView * imagev = [[UIImageView alloc] initWithFrame:CGRectMake(25, 15, 30, 30)];
        imagev.image = [UIImage imageNamed:@"topAnimation_device"];
        [cell.contentView addSubview:imagev];
    }
    
    [cell.contentView addSubview:titleLabel];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];

    if(indexPath.section == [[SurDataShareObj sharedInstance].groupArray count])
    {
        [self enterNewGroupController:nil];
    }
}

#pragma mark - private methods
-(void)enterNewGroupController:(SurGroupObject *)groupObj
{
    SurGroupOperationViewController * controller = [[SurGroupOperationViewController alloc] initWithNibName:@"SurGroupOperationViewController" bundle:nil];
    controller.datasource = self;
    controller.delegate = self;
    controller.groupObj = groupObj;
    [self.navigationController pushViewController:controller animated:YES];
}


-(IBAction)addDeviceButtonPressed:(id)sender
{
    UIButton * bt = (UIButton *)sender;
    SurGroupObject * obj = [SurDataShareObj sharedInstance].groupArray[bt.tag];
    [self enterNewGroupController:obj];
}

-(IBAction)deleteDeviceButtonPressed:(id)sender
{
    UIButton * bt = (UIButton *)sender;
    UITableViewCell *cell = nil;
    if([[[UIDevice currentDevice] systemVersion] intValue] >= 7)
    {
        cell = (UITableViewCell *)(bt.superview.superview.superview);
    }
    else
    {
        cell = (UITableViewCell *)(bt.superview.superview);
    }
    
    NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
    
    SurGroupObject * obj = [SurDataShareObj sharedInstance].groupArray[indexPath.section];
    NSDictionary * dic = nil;
    for(dic in [SurDataShareObj sharedInstance].allDeviceArray)
    {
        if([dic[@"name"] isEqualToString:obj.audioGroup[indexPath.row]])
        {
            [[GlobalService sharedInstance] addAudioToGroup:self baseURL:dic[@"ip"] groupName:@""];
            break;
        }
    }
}

#pragma mark - suroperation methods
-(NSArray *)getAllDevices
{
    return [SurDataShareObj sharedInstance].allDeviceArray;
}

-(void)devicesAddToGroup:(NSArray *)array
{
//    for(NSDictionary * dic in array)
//    {
//        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            [self dealWithComingDevice:dic];
//        });
//    }
}

-(void)newSurDataReceived
{
    [self.tableView reloadData];
}

-(void)serviceAddAudioToGroupCallBack:(NSDictionary*)dicCallBack
{

}
@end
